package query;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import document.Stemmer;
import document.InvertedIndex;


public class Term implements Query 
{
	
	private String name;
	protected List<Integer> queryResult;
	protected static Stemmer stemmer = new Stemmer();
	
	public Term(String name)
	{
		this.name = name;
	}

	@Override
	public List<Integer> getResult() 
	{
		return queryResult;
	}

	@Override
	public List<Query> getSubQueries() 
	{
		return null;
	}

	@Override
	public void process(InvertedIndex i) 
	{
		queryResult =  i.getPostingsDocId(stemmer.stem(name));
	}
	
	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public void putLeftOperand(Query q) {
		// do nothing
		
	}

	@Override
	public void putRightOperand(Query q) {
		// do nothing
		
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public boolean isNeutralQuery() {
		return false;
	}

	@Override
	public boolean isHomogenious() {
		return true;
	}

	@Override
	public Query rebuildOptimal(InvertedIndex i) 
	{
		return this;
	}

	@Override
	public Query rebuildWorst(InvertedIndex i) {
		return this;
	}

	@Override
	public List<Term> getLeaves() 
	{
		List<Term> result = new LinkedList<Term>();
		result.add(this);
		return result;
	}
	
	public static class TermAscending implements Comparator<Term>
	{

		@Override
		public int compare(Term arg0, Term arg1) {
			return arg0.getResult().size()-arg1.getResult().size();
		}
		
	}
	public static class TermDescending implements Comparator<Term>
	{

		@Override
		public int compare(Term arg0, Term arg1) {
			return arg1.getResult().size()-arg0.getResult().size();
		}
		
	}

	
}
